package com.idreems.model;

import java.util.LinkedList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.text.TextUtils;

import com.huewu.pla.pinterest.internal.ImageWrapper;

public class JionServerData extends ServerDataDecoder {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    protected static final int kArticlesPerPage = 50;
    protected String mTitle;
    protected String mUrl;
    
    
    protected String mRoot;
    protected String mDescription;
    protected String mThumbnailUrl;
    protected String mLargeUrl;
    
    public JionServerData() {
        mTitle = "爆笑囧图";
        mUrl = "http://i.snssdk.com/2/image/recent/?tag=heavy&count="+kArticlesPerPage;
        mRoot = "data";
        mDescription = "description";
        mThumbnailUrl = "thumbnail_url";
        mLargeUrl = "large_url";
    }

    @Override
    public String title() {
        return mTitle;
    }

    @Override
    public String url(int page) {
        if(page<=0)
        {
            page = 1;
        }
        mUrl = "http://i.snssdk.com/2/image/recent/?tag=heavy&count="+page*kArticlesPerPage;
        return mUrl;
    }

    @Override
    public List<ImageWrapper> decode(String data) {
        List<ImageWrapper> timeline = new LinkedList<ImageWrapper>();
        if (TextUtils.isEmpty(data)) {
            return timeline;
        }
        try {
            JSONObject obj = new JSONObject(data);
            JSONArray jTimeline = obj.getJSONArray(mRoot);
            for (int i = 0; i < jTimeline.length(); i++) {
                JSONObject element = jTimeline.getJSONObject(i);
                ImageWrapper wrapper = new ImageWrapper();

                wrapper.description = element.getString(mDescription);
                wrapper.url = element.getString(mThumbnailUrl);
                wrapper.largeUrl = element.getString(mLargeUrl);
                wrapper.id = i;
                timeline.add(wrapper);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return timeline;
    }
}
